<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin\AdminLink;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LinkController extends Controller
{
    //链接的视图
    public function index(){
        $links=AdminLink::all();
//        dd($links);
        return view ('admin/link')
            ->with('links',$links);
    }
    //添加友情链接的视图
    public function create(){
        return view ('admin.link_create');
    }
    //添加友情链接到数据库
    public function store(Request $request){
        $links= new AdminLink();
        $links->title=$request->title;
        $links->url=$request->url;
        if($links->save()){
            return redirect('admin/link');
        }else{
            return redirect()->back();
        }
    }
    //修改友情链接
    public function edit($id){

        $links=AdminLink::find($id);
//        dd($links);
        return view ('admin/link_edit')
              ->with('links',$links);
    }
    //修改友情链接到数据库
    public function update(Request $request,$id){
        $links=AdminLink::find($id);
        $links->title=$request->get('title');
        $links->url=$request->get('url');
        $links->save();
        return redirect('admin/link');

   }
   //删除友情链接
    public function destroy($id){
        $links = AdminLink::find($id);
        if($links->delete()){
            return redirect('admin/link');
        }else{
            return redirect()->back();
        }
    }
}
